package com.tedu.ds.sessionAndMessage.rabbitMq;

import com.tedu.ds.sessionAndMessage.config.RabbitFanoutConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class FanoutProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void fanoutMessage(String msg)throws Exception{
        log.info("开始发送消息: {}", msg);
        // 创建持久化的消息
        Message message = MessageBuilder.withBody(msg.getBytes("UTF-8"))
                .setDeliveryMode(MessageDeliveryMode.PERSISTENT)  // 设置消息为持久化
                .build();
        rabbitTemplate.send(RabbitFanoutConfig.FANOUT_EXCHANGE_NAME,null,message);
    }
}
